/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;
import Modele.Connect;
import Modele.Medecin;
import Modele.Pays;
import Modele.Specialite;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Map;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sio
 */
public class Control extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private Pays pays;
    
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        pays=new Pays();
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uc=request.getParameter("uc");
        String page=null;
        switch (uc){
            case"dep":
                if(request.getParameter("selectDep")==null){
                    page="dep.jsp"; 
                    request.setAttribute("listeDep",pays.getLesDeps());
                }
                else{
                    String num=request.getParameter("selectDep");
                    Collection<Medecin> lesMeds=pays.getLeDep(num).getLesMedecins();
                    request.setAttribute("lesMeds",lesMeds);
                    page="meds.jsp";
                }       
                break;
            case"nomed":
                if(request.getParameter("nom")==null){
                    page="nomed.jsp";
                }
                else{
                    Collection<Medecin> lesMeds=pays.getMedsByNom(request.getParameter("nom"));
                    request.setAttribute("lesMeds",lesMeds);
                    page="mednom.jsp";
                }
                break;
            case"spe":
                if(request.getParameter("selectSpe")==null){
                    Collection<Specialite> lesSpe=pays.getLesSpe();
                    request.setAttribute("lesSpe",lesSpe);
                    page="lesspe.jsp";
                }
                else{
                    String nomSpe=request.getParameter("selectSpe");
                    Collection<Medecin> lesMeds=pays.getLaSpe(nomSpe).getLesMeds();
                    request.setAttribute("lesMeds",lesMeds);
                    page="medspe.jsp";
                }
                
        }
        request.getRequestDispatcher(page).forward(request,response);
        
    }
    
    public void destroy(){
        Connect.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
